#### 接收端的代码
import network
import espnow
import test
def init_espnow(send_mac,screen):
    
    # A WLAN interface must be active to send()/recv()
    sta = network.WLAN(network.STA_IF)
    sta.active(True)
    sta.disconnect()                # Disconnect from last connected WiFi SSID
    screen.text('wlan created',0,0,1)
    screen.show()

    e = espnow.ESPNow()                  # Enable ESP-NOW
    e.active(True)

    screen.text('espnow created',0,8,1)
    screen.show()

    peer = send_mac   # MAC address of peer's wifi interface
    #这个MAC地址是发送端的
    e.add_peer(peer)                     # Sender's MAC registration

    screen.text('get send mac',0,16,1)
    screen.show()
    return e
def espnow_rx(e,screen):
    while True:
        host, msg = e.recv()
        msg=msg.decode()
        if msg:                          # wait for message
            print(msg)
            screen.fill(0)
            screen.text(msg,0,0,1)
            screen.show()
            if 'angle' in msg:
                angle=msg.split('#')[1]
                print(angle)
                screen.fill(0)
                screen.text("angle is "+angle,0,0,1)
                screen.show()
                test.direction(int(angle))
            if 'run' in msg:
                run_dir=msg.split('#')[1]
                run_speed=msg.split('#')[2]
                if run_dir=='front':
                    print(run_speed)
                    screen.fill(0)
                    screen.text("run is front "+run_speed,0,0,1)
                    screen.show()
                    test.run(0,int(run_speed))
                elif run_dir=='back':
                    print(run_speed)
                    screen.fill(0)
                    screen.text("run is back "+run_speed,0,0,1)
                    screen.show()
                    test.run(int(run_speed),0)
            
if __name__ == "__main__":
    screen=test.init_display()
    send_mac=b'x\xe3mxN('
    e=init_espnow(send_mac,screen)
    espnow_rx(e,screen)
